/*package com.jmpt.fangbohui.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RatingBar;
import android.widget.TextView;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapPoi;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.Marker;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jmpt.fangbohui.ui.base.BaseActivity;

import java.util.List;

public class MapActivity extends BaseActivity {
    public static final String EXTRAS_DATA = "extras_data";

    public static final String EXTRAS_BANNER_DATA = "extras_bannaer_data";
    public static final String EXTRAS_SHOP_DETAIL = "extras_shopdetail";


    private boolean isShopDetail = false;

    private List<HomeBean.Category> mCategorys;
    private List<StoreBean> mStoreBean;

    private BaiduMap mBaiduMap;
    private LocationClient mLocClient;
    public boolean isFirstLoc = true;
    LatLng ll;
    MyLocationData locData;

    private View popStoreDetailView;
    private InfoWindow mInfoWindow;
    private int mCurrenCategoryIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

           set it to be no title
        requestWindowFeature(Window.FEATURE_NO_TITLE);

       set it to be full screen
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_map);
        assignViews();

        popStoreDetailView = View.inflate(mContext, R.layout.pop_map_store, null);
        assignViews(popStoreDetailView);
        popStoreDetailView.setLayoutParams(new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        mBaiduMap = mMap.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        if (getIntent().hasExtra(EXTRAS_DATA)) {
            mStoreBean = new Gson().fromJson(getIntent().getExtras().getString(EXTRAS_DATA), new TypeToken<List<StoreBean>>() {
            }.getType());

        }
        if (mStoreBean != null) {
            for (int i = 0; i < mStoreBean.size(); i++) {
                if (TextUtils.isEmpty(mStoreBean.get(i).lat) == false) {
                    DebugLog.i("mStoreBean.get(i)" + new Gson().toJson(mStoreBean.get(i)));
                    addImageToMap(mStoreBean.get(i));
                }
            }
        }

        if (getIntent().hasExtra(EXTRAS_BANNER_DATA)) {
            mCategorys = new Gson().fromJson(getIntent().getExtras().getString(EXTRAS_BANNER_DATA), new TypeToken<List<HomeBean.Category>>() {
            }.getType());
        }

        if (getIntent().hasExtra(EXTRAS_SHOP_DETAIL)) {
            isShopDetail = getIntent().getExtras().getBoolean(EXTRAS_SHOP_DETAIL, false);
        }
        if (isShopDetail) {
            ll = new LatLng(Double.parseDouble(mStoreBean.get(0).lat),
                    Double.parseDouble(mStoreBean.get(0).lng));
            isFirstLoc = false;
            MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
            mBaiduMap.animateMapStatus(u);
        }

        MapStatusUpdate mapStatusUpdate = MapStatusUpdateFactory.zoomTo(18f);
        mBaiduMap.setMapStatus(mapStatusUpdate);
        mLocClient = new LocationClient(this);
        mLocation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (ll != null) {
                    MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                    mBaiduMap.animateMapStatus(u);
                }
            }
        });
        mBaiduMap.setOnMapClickListener(new BaiduMap.OnMapClickListener() {
            @Override
            public void onMapClick(LatLng latLng) {
                mBaiduMap.hideInfoWindow();
            }

            @Override
            public boolean onMapPoiClick(MapPoi mapPoi) {
                return false;
            }
        });
        location();

    }

    private RatingBar mShopRatingBar;

    private TextView mName;

    private void setmCategory() {
        new Gson().toJson(mCategorys);
        mCategory.setVisibility(View.VISIBLE);
        HomeBean.Category category = new HomeBean().new Category();
        category.cate_id = 0;
        category.cate_name = "全部";
        mCategorys.add(0, category);
        mCategory.setText(mCategorys.get(0).cate_name);
        mCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
                CharSequence[] charSequences = new CharSequence[mCategorys.size()];
                for (int i = 0; i < mCategorys.size(); i++) {
                    charSequences[i] = mCategorys.get(i).cate_name;
                }
                builder.setSingleChoiceItems(charSequences, mCurrenCategoryIndex, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        if (mCurrenCategoryIndex != i) {
                            mCurrenCategoryIndex = i;
                            dialogInterface.dismiss();
                            mCategory.setText(mCategorys.get(i).cate_name);
                            addRequestHandle(ApiRequest.getShopNear(mContext, String.valueOf(locData.longitude), String.valueOf(locData.latitude), mCategorys.get(i).cate_id, new HttpStoreListHandler(mContext)));
                        }
                    }
                });
                builder.setCancelable(true);
                builder.show();
            }
        });
        addRequestHandle(ApiRequest.getShopNear(mContext, String.valueOf(locData.longitude), String.valueOf(locData.latitude), mCategorys.get(0).cate_id, new HttpStoreListHandler(mContext)));
    }

    private void assignViews(View view) {
        mShopRatingBar = (RatingBar) view.findViewById(R.id.shopRatingBar);

        mName = (TextView) view.findViewById(R.id.name);
    }

    class HttpStoreListHandler extends JsonHttpHandler {

        public HttpStoreListHandler(Context context) {
            super(context);
            showProgressDialog();
        }

        @Override
        public void onDo(String response) {
            super.onDo(response);

            List<StoreBean> entities = new Gson().fromJson(response, new com.google.common.reflect.TypeToken<List<StoreBean>>() {
            }.getType());
            mBaiduMap.clear();
            for (int i = 0; i < entities.size(); i++) {
                if (TextUtils.isEmpty(entities.get(i).lat) == false) {
                    addImageToMap(entities.get(i));
                }
            }
        }
    }

    private void addImageToMap(final StoreBean storeBean) {
//        BitmapDescriptor bitmap=null;
//        DebugLog.i("storeBean.map_mark:"+storeBean.map_mark);
//        try {
//            bitmap = BitmapDescriptorFactory.fromBitmap(ImageLoaderHelper.getImageLoader().loadImageSync(storeBean.map_mark, ImageLoaderHelper.getBaseDisplayImageOptions()));
//
//        }catch (Exception e){
//            e.printStackTrace();
//
//        }
//        if(bitmap==null){
//            bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_food);
//        }
//            addImageToMap(storeBean,  bitmap  );
        try {
            new LoadMarkTask().execute(storeBean, storeBean);
        } catch (Exception e) {
            e.printStackTrace();

        }
    }

    class LoadMarkTask extends AsyncTask<StoreBean, Void, StoreBean> {

        @Override
        protected StoreBean doInBackground(StoreBean... params) {
            try {
                ImageLoaderHelper.getImageLoader().loadImageSync(params[0].map_mark, ImageLoaderHelper.getBaseDisplayImageOptions());
            } catch (Exception e) {
                e.printStackTrace();
            }
            return params[0];
        }

        @Override
        protected void onPostExecute(StoreBean storeBean) {
            super.onPostExecute(storeBean);
            BitmapDescriptor bitmap = BitmapDescriptorFactory.fromBitmap(ImageLoaderHelper.getImageLoader().loadImageSync(storeBean.map_mark, ImageLoaderHelper.getBaseDisplayImageOptions()));
            if (bitmap == null) {
                bitmap = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_food);
            }
            addImageToMap(storeBean, bitmap);

        }
    }


    private void addImageToMap(StoreBean storeBean, BitmapDescriptor bitmap) {
        LatLng point = new LatLng(Double.parseDouble(storeBean.lat), Double.parseDouble(storeBean.lng));
        //构建Marker图标

        Bundle bundle = new Bundle();
        bundle.putString("data", new Gson().toJson(storeBean));

        //构建MarkerOption，用于在地图上添加Marker

        OverlayOptions option = new MarkerOptions()
                .position(point)
                .icon(bitmap).extraInfo(bundle);


        //在地图上添加Marker，并显示
        mBaiduMap.addOverlay(option);
        mBaiduMap.setOnMarkerClickListener(new BaiduMap.OnMarkerClickListener() {
            @Override
            public boolean onMarkerClick(Marker marker) {
                final StoreBean bean = new Gson().fromJson(marker.getExtraInfo().getString("data"), StoreBean.class);
                DebugLog.i("bean.store_name:" + bean.store_name);
                mName.setText(bean.store_name);
                mName.setVisibility(View.VISIBLE);
                mShopRatingBar.setRating(bean.praise_rate_int);
                // mPhoto.setImageResource(R.color.transparent);
                //ImageLoaderHelper.displayImage(bean.store_logo,mPhoto);

                InfoWindow.OnInfoWindowClickListener listener = new InfoWindow.OnInfoWindowClickListener() {
                    @Override
                    public void onInfoWindowClick() {
                        Intent intent = new Intent(mContext, StoreDetailActivity.class);
                        intent.putExtra(StoreDetailActivity.EXTRAS_STORE_ID, bean.store_id);
                        startActivity(intent);
                        mBaiduMap.hideInfoWindow();
                    }

                };
                LatLng ll = marker.getPosition();
                mInfoWindow = new InfoWindow(BitmapDescriptorFactory.fromView(popStoreDetailView), ll, -72, listener);
                mBaiduMap.showInfoWindow(mInfoWindow);
                return true;
            }
        });
    }

    private void location() {
        mBaiduMap.setMyLocationEnabled(true);

        // 定位初始化
        mLocClient.registerLocationListener(new MyLocationListenner());
        mLocClient.start();
    }


    public class MyLocationListenner implements BDLocationListener {

        @Override
        public void onReceiveLocation(BDLocation location) {
            // map view 销毁后不在处理新接收的位置
            if (location == null || mMap == null)
                return;
            locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                            // 此处设置开发者获取到的方向信息，顺时针0-360
                    .direction(100).latitude(location.getLatitude())
                    .longitude(location.getLongitude()).build();
            mBaiduMap.setMyLocationData(locData);
            ll = new LatLng(location.getLatitude(),
                    location.getLongitude());
            if (isFirstLoc) {
                isFirstLoc = false;
                MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll);
                mBaiduMap.animateMapStatus(u);
            }
            if (mCategorys != null && mCategorys.size() > 0) {
                setmCategory();
            }
        }

        public void onReceivePoi(BDLocation poiLocation) {
        }
    }

    private MapView mMap;
    private ImageButton mLocation;
    private Button mCategory;

    private void assignViews() {
        mMap = (MapView) findViewById(R.id.map);
        mLocation = (ImageButton) findViewById(R.id.location);
        mCategory = (Button) findViewById(R.id.category);
    }


}
*/